package com.ruoyi.mall.mapper;

import com.ruoyi.mall.domain.Goods;
import com.ruoyi.mall.domain.ProductSkus;
import com.ruoyi.mall.domain.ProductSpecValues;
import com.ruoyi.mall.domain.Specifications;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.Date;
import java.util.List;

/**
 * 商品信息Mapper接口
 *
 * @author ruoyi
 * @date 2025-08-13
 */
public interface GoodsMapper {
    /**
     * 查询商品信息
     *
     * @param id 商品信息主键
     * @return 商品信息
     */
    public Goods selectGoodsById(Long id);

    /**
     * 查询商品信息列表
     *
     * @param goods 商品信息
     * @return 商品信息集合
     */
    public List<Goods> selectGoodsList(Goods goods);

    /**
     * 新增商品信息
     *
     * @param goods 商品信息
     * @return 结果
     */
    public int insertGoods(Goods goods);

    /**
     * 修改商品信息
     *
     * @param goods 商品信息
     * @return 结果
     */
    public int updateGoods(Goods goods);

    /**
     * 删除商品信息
     *
     * @param id 商品信息主键
     * @return 结果
     */
    public int deleteGoodsById(Long id);

    /**
     * 批量删除商品信息
     *
     * @param ids 需要删除的数据主键集合
     * @return 结果
     */
    public int deleteGoodsByIds(Long[] ids);

    @Select("SELECT id from t_goods where name=#{name}")
    Long selectId(@Param("name") String name);

    @Select("SELECT img_show as imgShow,name from t_goods where id=#{id}")
    Goods selectOrder(@Param("id") Integer id);

    @Select("SELECT spec_id as specId,goods_id as goodsId,spec_name as specName FROM t_specifications WHERE t_specifications.goods_id=#{id}")
    List<Specifications> selSpec(@Param("id") Integer id);

    @Select("SELECT spec_value_id as specValueId,spec_id as specId,spec_value as specValue FROM t_product_spec_values WHERE spec_id=#{id}")
    List<ProductSpecValues> selProd(@Param("id") Integer id);

    @Select("SELECT spec_value_id FROM `t_product_spec_values` where spec_id in(select spec_id from t_specifications WHERE goods_id=#{id}) and spec_value=#{name}\n")
    Integer selProdId(@Param("id") Integer id, @Param("name") String name);

    @Select("SELECT sku_id as skuId,img,product_id as productId,sku_code as skuCode,price,stock,ticket_num as ticketNum FROM t_product_skus WHERE sku_code=#{code}")
    ProductSkus selSkuCode(@Param("code") String code);

    @Select("SELECT spec_id as specId,goods_id as goodsId,spec_name as specName FROM t_specifications WHERE spec_id=#{id}")
    Specifications selOldSpec(@Param("id") Integer id);

    @Select("SELECT spec_value_id as specValueId,spec_id as specId,spec_value as specValue FROM t_product_spec_values WHERE spec_value_id=#{id}")
    ProductSpecValues selOldSpecValue(@Param("id") Integer id);

    @Update("UPDATE t_specifications SET spec_name=#{name},update_time=#{date} WHERE spec_id=#{id}")
    int updateSpec(@Param("id") Integer id, @Param("name") String name, @Param("date") Date date);

    @Update("UPDATE t_product_spec_values SET spec_value=#{name},update_time=#{date} WHERE spec_value_id=#{id}")
    int updateSpecValue(@Param("id") Integer id, @Param("name") String name, @Param("date") Date date);

    @Delete({"<script>", "delete from t_specifications where goods_id=#{id} and spec_id not in" + "<foreach item='item' index='index' collection='list' open='(' separator=',' close=')'>", "#{item}", "</foreach>", "</script>"})
    int deleGGid(@Param("id") Integer id, @Param("list") List<Integer> list);

    @Delete({"<script>", "delete from t_product_spec_values where spec_id in (select spec_id from t_specifications where goods_id=#{id}) and spec_value_id not in" + "<foreach item='item' index='index' collection='list' open='(' separator=',' close=')'>", "#{item}", "</foreach>", "</script>"})
    int deleGGZid(@Param("id") Integer id, @Param("list") List<Integer> list);


    @Delete({"<script>", "delete from t_product_skus where product_id=#{id} and sku_id not in" + "<foreach item='item' index='index' collection='list' open='(' separator=',' close=')'>", "#{item}", "</foreach>", "</script>"})
    int deleSku(@Param("id") Integer id, @Param("list") List<Integer> list);

    @Select("SELECT count(*) from t_goods where `name`=#{name} and is_delete=0")
    int selCount(@Param("name") String name);
}
